package com.uenpay.im.ui.old.activity.movement;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class GetMovementMembersListInvokeItem extends BaseHttpInvokeItem {
	public class GetMovementMembersListInvokeItemResult extends HttpInvokeResult {
		ArrayList<QYXUserEntity> arrayList = new ArrayList<QYXUserEntity>();
	}

	public GetMovementMembersListInvokeItemResult getOutput() {
		return (GetMovementMembersListInvokeItemResult) GetResultObject();
	}

	public GetMovementMembersListInvokeItem(int page,int activityid) {
		String url =  APIConfiguration.getBaseUrl()
					+ "Activity/Activity/getMemberList?activityid="+activityid+"&pageIndex=" + page + "&pageSize=12&"
					+ APIConfiguration.getCustIdAndToken();

		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetMovementMembersListInvokeItemResult result = new GetMovementMembersListInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}

		JSONArray arr = json.optJSONArray("data");
		if (arr != null) {
			for (int i = 0, size = arr.length(); i < size; i++) {
				result.arrayList.add(desQYXUserEntity(arr.optJSONObject(i)));
			}
		}

		return result;
	}

	private QYXUserEntity desQYXUserEntity(JSONObject json) {
		QYXUserEntity entity = new QYXUserEntity();
		entity.custid = json.optString("custid");
		entity.nickname = json.optString("nickname");
		entity.sex = json.optInt("sex");
		entity.status = json.optInt("status");
		return entity;
	}

}
